const vcity = {
	11: "北京",
	12: "天津",
	13: "河北",
	14: "山西",
	15: "内蒙古",
	21: "辽宁",
	22: "吉林",
	23: "黑龙江",
	31: "上海",
	32: "江苏",
	33: "浙江",
	34: "安徽",
	35: "福建",
	36: "江西",
	37: "山东",
	41: "河南",
	42: "湖北",
	43: "湖南",
	44: "广东",
	45: "广西",
	46: "海南",
	50: "重庆",
	51: "四川",
	52: "贵州",
	53: "云南",
	54: "西藏",
	61: "陕西",
	62: "甘肃",
	63: "青海",
	64: "宁夏",
	65: "新疆",
	71: "台湾",
	81: "香港",
	82: "澳门",
	91: "国外"
};

/**
 * 验证身份证合法性
 * @param cardId  身份证号码  String类型
 * @returns  boolean true合法,false不合法
 */
// 检查基本输入规则
const isCardNo = cardId => {
	const reg = /(^\d{17}(\d|X|x)$)/;
	return reg.test(cardId);
};

// 检测前两位是否为省份
const checkProvince = cardId => {
	const province = cardId.substr(0, 2);
	return Boolean(vcity[province]);
};

// 检测日期
const verifyBirthday = (year, month, day, birthday) => {
	const now = new Date();
	const now_year = now.getFullYear();
	// 判断年月日是否合理
	if (
		birthday.getFullYear() == year &&
		birthday.getMonth() + 1 == month &&
		birthday.getDate() == day
	) {
		// 判断年份的范围（3岁到100岁之间)
		const time = now_year - year;
		return time >= 3 && time <= 100;
	}
	return false;
};
// 检测生日是否正确
const checkBirthday = cardId => {
	const re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)$/;
	const arr_data = cardId.match(re_eighteen);
	const year = arr_data[2];
	const month = arr_data[3];
	const day = arr_data[4];
	const birthday = new Date(`${year}/${month}/${day}`);
	return verifyBirthday(year, month, day, birthday);
};
// 检测检验位是否正确
const checkParity = cardId => {
	const arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
	const arrCh = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
	let cardTemp = 0,
		i,
		valnum;
	for (i = 0; i < 17; i++) {
		cardTemp += cardId.substr(i, 1) * arrInt[i];
	}
	valnum = arrCh[cardTemp % 11];
	// console.log('身份证检验位应该是: ', valnum);
	return valnum == cardId.substr(17, 1);
};

/**
 * 根据身份证号码验证是否小于22岁，不含22岁
 */

export const checkIdCardAge = idCard => {
	let isLesstt = false; //是否小于22，false：大于/true:小于
	const re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X|x)$/;
	const arr_data = idCard.match(re_eighteen);
	const cardYear = arr_data[2];
	//const cardMonth = arr_data[3];
	//const cardDay = arr_data[4];

	const nowYear = new Date().getFullYear();
	//const nowMonth = new Date().getMonth() + 1;
	//const nowDay = new Date().getDate();
	let cyear = nowYear - cardYear; //相差的年份
	if (cyear <= 21 && cyear >= 18) {
		isLesstt = true;
	} else {
		isLesstt = false;
	}
	// if (cyear > 22 || cyear < 18) {
	//     isLesstt = false;
	// } else if (cyear == 22) {
	//     if (cardMonth < nowMonth) { //生日月份过了，满了22岁
	//         isLesstt = false;
	//     } else if (cardMonth == nowMonth) {
	//         if (cardDay < nowDay) {  //生日天过了，满了22岁
	//             isLesstt = false;
	//         } else {
	//             isLesstt = true;
	//         }
	//     } else {
	//         isLesstt = true;
	//     }
	// } else if (cyear == 18) {
	//     if (cardMonth < nowMonth) { //生日月份过了，满了18岁
	//         isLesstt = true;
	//     } else if (cardMonth == nowMonth) {
	//         if (cardDay < nowDay) {  //生日天过了，满了18岁
	//             isLesstt = true;
	//         } else {
	//             isLesstt = false;
	//         }
	//     } else {
	//         isLesstt = false;
	//     }
	// } else {
	//     isLesstt = true;
	// }
	return isLesstt;
};

export const checkTheIdCard = cardId => {
	// 检测身份证是否合法
	return !!(
		isCardNo(cardId) &&
		checkProvince(cardId) &&
		checkBirthday(cardId) &&
		checkParity(cardId)
	);
};
